package leetcode.day;

import java.util.Arrays;

// 5991. 按符号重排数组
public class RearrangeArray {
    public static void main(String[] args) {
        int[] d = {3,1,-2,-5,2,-4};
        System.out.println(Arrays.toString(rearrangeArray(d)));
    }

    public static int[] rearrangeArray(int[] nums) {
        int[] d1 = new int[nums.length / 2];
        int[] d2 = new int[nums.length / 2];
        int index1 = 0, index2 = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                d1[index1++] = nums[i];
            } else if (nums[i] < 0) {
                d2[index2++] = nums[i];
            }
        }
        int[] res = new int[nums.length];
        int p=0,q=0;
        for (int i = 0; i < nums.length; i+=2) {
            res[i]= d1[p++];
            res[i+1] = d2[q++];
        }

        return res;
    }
}
